/* ****************************************************************************
buffer.h
-------------------------------------------------------------------------------

Copyright (c) 2017, Tain L.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************** */

#if !defined(SPEARS_SPEARS_BUFFER_H)
#define SPEARS_SPEARS_BUFFER_H

#include "../my-prerequisites.h"

namespace Spears
{
	class Buffer
	{
	public:
		Buffer(const size_t& sizeInBytes);
		virtual ~Buffer();

		// deleted:
		explicit Buffer(const Buffer& rhs) = delete;
		explicit Buffer(Buffer&& rhs) = delete;

	public:
		void write(const size_t& offset, void const * const pSrc, const size_t& size);
		void read(void * const pDist, const size_t& offset, const size_t& size) const;
		const size_t size() const;
		void* buffer() const;

	private:
		size_t const	m_size;
		byte * const	m_p_data;
	};

	inline Buffer::Buffer(const size_t& sizeInBytes) :
		m_size(sizeInBytes), m_p_data(new byte[sizeInBytes])
	{}

	inline Buffer::~Buffer()
	{
		if (m_p_data != nullptr)
			delete[] m_p_data;
	}

	inline void Buffer::write(const size_t& offset, void const * const pSrc, const size_t& size)
	{
		byte*	pStart = const_cast<byte*>(m_p_data);
		pStart += offset;
		memcpy(pStart, pSrc, size);
	}

	inline void Buffer::read(void * const pDist, const size_t& offset, const size_t& size) const
	{
		byte*	pStart = const_cast<byte*>(m_p_data);
		pStart += offset;
		memcpy(pDist, pStart, size);
	}

	inline const size_t Buffer::size() const
	{
		return m_size;
	}

	inline void* Buffer::buffer() const
	{
		return m_p_data;
	}

} // namespace Spear

#endif // SPEARS_SPEARS_BUFFER_H